﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resehantering.ServiceReference1;

namespace Resehantering
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.IsAuthenticated && User.IsInRole("admin"))
            {
                Response.Redirect("/Account/Register.aspx");
            }
            else if (Request.IsAuthenticated && User.IsInRole("boss"))
            {
                Response.Redirect("~/boss.aspx");
            }
            /*else if (!Request.IsAuthenticated)
            {
                Response.Redirect("/Account/login.aspx");
            }*/
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int handlaggare = Convert.ToInt32(DropDownList1.SelectedValue);
            int konto = Convert.ToInt32(DropDownList2.SelectedValue);
            double belopp = Convert.ToDouble(TextBox2.Text);
            string anledning = TextBox1.Text;

            string user = User.Identity.Name;
            if(user == ""){ //säkrar att det finns ett namn på sparat reseförskott
                user = "Default name";
            }

            if (handlaggare.ToString() != "" && konto.ToString() != "" && belopp.ToString() != "" && anledning != "")
            {
                ServiceReference1.Service1Client wcfClient = new ServiceReference1.Service1Client();
                ReseForskott forskott = new ReseForskott(1, false, belopp, anledning, handlaggare, konto, user);

                List<ReseForskott> row = new List<ReseForskott>();
                row.Add(forskott);

                wcfClient.SetReseForskott(row);

                Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'><SCRIPT language=\"JavaScript\">alert('Ditt reseförskott har registrerats!');</SCRIPT>");
            }
            else
            {
                Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'><SCRIPT language=\"JavaScript\">alert('Fylla i alla fält!');</SCRIPT>");
            }
        }
    }
}
